package oop.ex2.expression;

import java.util.HashMap;

/**
 * This stores a collection of variables and supports operations such as adding a variable and
 * changing a value.
 * 
 * This specific implementation uses a HashMap.
 * @author daniel_i_l
 *
 */
public class VariableTable {
	private HashMap<String, Double> _variableMap;
	
	/**
	 * Construct an empty variable table.
	 */
	public VariableTable()
	{
		_variableMap = new HashMap<String, Double>();
	}
	
	/**
	 * Check if a variable exists.
	 * @param varName The variable to check.
	 * @return True if the variable exists and False otherwise.
	 */
	public boolean hasVariable(String varName)
	{
		return _variableMap.containsKey(varName);
	}
	
	/**
	 * Get the value of an existing variable.
	 * This assumes that the user already knows that the variable exists.
	 * @param varName The variable to return.
	 * @return The value of varName.
	 */
	public double getValue(String varName)
	{
		return _variableMap.get(varName).doubleValue();
	}
	
	/**
	 * If the variable doesn't exist, add it with the given  value. Otherwise, update the value
	 * of the existing variable.
	 * @param varName The variable to set.
	 * @param value The value to set it to.
	 */
	public void setValue(String varName, double value)
	{
		_variableMap.put(varName, value);
	}
}
